package lx.scrmHlkj;

import com.alibaba.fastjson.JSONObject;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import lx.util.HttpHelper;
import lx.util.StrUtil;

import java.text.MessageFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimerTask;

/**
 * @Description: 汇龙科技线上token定时器
 * @Author: 李星 lixing_java@163.com
 * @Date: 2020/3/14 10:47
 */
public class HlkjTokenTask extends TimerTask {

    @Override
    public void run() {
        // 删除所有token
        System.out.println(MessageFormat.format("删除token结果：{0}", Db.delete("DELETE FROM XJ_SCRM_TOKEN WHERE CATEGORY IN (1,2,3)")));
        System.out.println();
        // 获取指定token
        getToken(1); // 通讯录
        getToken(2); // 外部联系人
//        getToken(3); // 外部联系人
        getToken(4); // 自建应用模块(lixing)
    }

    /*
     * @Description 获取指定类型的TOKEN
     * @Author lixing
     * @Date 2020/4/22 9:37
     * @Param [category TOKEN的类型]
     * @return void
     */
    public void getToken(int category) {
        String tokenCategory;
        String tokenParam;
        if (category == 1) {
            tokenCategory = "通讯录模块";
            tokenParam = "corpid=wx313186dcb4bda779&corpsecret=RKjkZnLZOHjdMGRz2FHch43dnhxshnro_PfYdNVdTP8";
        } else if (category == 2) {
            tokenCategory = "外部联系人";
            tokenParam = "corpid=wx313186dcb4bda779&corpsecret=uPb1L5ohWeScNVRovf1gBA2ALdt9leQjswDIEvHNR6k";
        } else if (category == 3) {
            tokenCategory = "应用消息";
            tokenParam = "";
        } else if (category == 4) {
            tokenCategory = "自建应用模块(lixing)";
            tokenParam = "corpid=wx313186dcb4bda779&corpsecret=XfByxZZo6yV38ucVSk3bp4ZR-tLoFu4lbyOUiCSXfT4";
        } else {
            tokenCategory = "TOKEN类型无效";
            tokenParam = "";
        }

        System.out.println(MessageFormat.format("getToken----{0} {1}", tokenCategory, new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())));
        String result_token = HttpHelper.sendRequestHttps(
                MessageFormat.format("https://qyapi.weixin.qq.com/cgi-bin/gettoken?{0}", tokenParam),
                "GET",
                null);
        System.out.println(MessageFormat.format("从微信服务器获取Token返回值：{0}", result_token));
        if (StrUtil.isEffective(result_token)) {
            try {
                JSONObject result = JSONObject.parseObject(result_token);
                if ("0".equals(result.getString("errcode")) && "ok".equals(result.getString("errmsg"))) {
                    String access_token = result.getString("access_token");
                    int expires_in = result.getInteger("expires_in");
                    //
                    Record record_tokenUrlForTxl = new Record();
                    record_tokenUrlForTxl.set("ACCESS_TOKEN", access_token);
                    record_tokenUrlForTxl.set("EXPIRES_IN", expires_in);
                    record_tokenUrlForTxl.set("CATEGORY", category);
                    boolean flag = Db.save("XJ_SCRM_TOKEN", "ACCESS_TOKEN", record_tokenUrlForTxl);
                    System.out.println(MessageFormat.format("添加到本地：{0}", flag));
                } else {
                    System.out.println(MessageFormat.format("从微信服务器获取Token失败：{0}", JSONObject.toJSONString(result)));
                }
            } catch (Exception e) {
                System.out.println(MessageFormat.format("从微信服务器获取Token失败：{0}", e.getMessage()));
            }
        } else {
            System.out.println("返回值为空");
        }
        System.out.println();
    }

}
